{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "0\n",
      "1\n",
      "2\n",
      "    \n",
      "0\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def generator(n):\n",
    "    while True:\n",
    "        for j in range(n):\n",
    "            yield j\n",
    "\n",
    "def function(n):\n",
    "    for j in range(n):\n",
    "        return j\n",
    "\n",
    "ourGen = generator(3)\n",
    "ourFun = function(3)\n",
    "\n",
    "\n",
    "print(ourGen.__next__())\n",
    "print(ourGen.__next__())\n",
    "print(next(ourGen))\n",
    "print(next(ourGen))\n",
    "print(next(ourGen))\n",
    "print(next(ourGen))\n",
    "print('    ')\n",
    "print(ourFun)\n",
    "print(ourFun)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Light turned GREEN at t=0\n",
      "Light turned YELOOW at t=30\n",
      "Light turned RED at t=35\n",
      "Light turned GREEN at t=55\n",
      "Light turned YELOOW at t=85\n",
      "Light turned RED at t=90\n",
      "Light turned GREEN at t=110\n",
      "Simulation complete\n"
     ]
    }
   ],
   "source": [
    "# 红绿灯案例：\n",
    "\n",
    "import simpy\n",
    "\n",
    "\n",
    "def main():\n",
    "    env = simpy.Environment()\n",
    "    env.process(traffic_light(env))\n",
    "    env.run(until=120)  # 设置最多运行的时间\n",
    "    print(\"Simulation complete\")\n",
    "\n",
    "# define a process\n",
    "def traffic_light(env):\n",
    "    while True:\n",
    "        print(\"Light turned GREEN at t=\"+str(env.now))\n",
    "        yield env.timeout(30)  # 并不会真的等待30s，而是假装等待了30s的“仿真时间”，实际上是立跳到下面\n",
    "        print(\"Light turned YELOOW at t=\"+str(env.now))\n",
    "        yield env.timeout(5)\n",
    "        print(\"Light turned RED at t=\"+str(env.now))\n",
    "        yield env.timeout(20)\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "schedule",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
